/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backing;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.CurrentUser;
import officeboost.domain.*;
import officeboost.entity.form.LeaveForm;

/**
 *
 * @author XJ
 */
@ManagedBean
public class LeaveBacking extends AbstractBacking {

    @EJB
    private LogMgr logger;
    @EJB
    private OfficeMgr officeMgr;
    @EJB
    private PersonService personService;
    @ManagedProperty(value = "")
    private String id;
    @ManagedProperty(value = "")
    private String name;
    @ManagedProperty(value = "")
    private String time_span;
    @ManagedProperty(value = "")
    private String reason;
    @ManagedProperty(value = "")
    private String teacher_opinion;
  
    @ManagedProperty(value = "")
    private String student_office_opinion;
 
    @ManagedProperty(value = "")
    private String education_office_opinion;

    @ManagedProperty(value = "")
    private String school_opinion;

    private String decision;
    private String[] disabled = new String[5];

    public String getDecision() {
        return decision;
    }

    public void setDecision(String decision) {
        this.decision = decision;
    }

    public String[] getDisabled() {
        return disabled;
    }

    public void setDisabled(String[] disabled) {
        this.disabled = disabled;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEducation_office_opinion() {
        return education_office_opinion;
    }

    public void setEducation_office_opinion(String education_office_opinion) {
        this.education_office_opinion = education_office_opinion;
    }

    public String getName() {
        return name;
    }

    public void setName(String username) {
        this.name = username;
        // this.name=getCurrentUser().getUsername();
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getSchool_opinion() {
        return school_opinion;
    }

    public void setSchool_opinion(String school_opinion) {
        this.school_opinion = school_opinion;
    }

    public String getStudent_office_opinion() {
        return student_office_opinion;
    }

    public void setStudent_office_opinion(String student_office_opinion) {
        this.student_office_opinion = student_office_opinion;
    }

    public String getTeacher_opinion() {
        return teacher_opinion;
    }

    public void setTeacher_opinion(String teacher_opinion) {
        this.teacher_opinion = teacher_opinion;
    }

    public String getTime_span() {
        return time_span;
    }

    public void setTime_span(String time_span) {
        this.time_span = time_span;
    }

    public boolean setAuthority() {
        boolean outcome = false;

        return outcome;
    }



    /**
     * Creates a new instance of LeaveBacking
     */
    public String applyForm() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest myRequest = (HttpServletRequest) context.getExternalContext().getRequest();
        //      HttpSession mySession = myRequest.getSession(); 
        String myParam = myRequest.getParameter("myparam");
        String mytaskid = myRequest.getParameter("mytaskid");

      //  logger.log(mytaskid + "taskid WAR:apply leave form");
      //  this.logger.log(myParam + "action WAR: apply leave form");
        CurrentUser cu = (CurrentUser) getSessionMap().get("currentUser");

        if (myParam.equals("apply")) {
            LeaveForm lf = new LeaveForm();
            lf.setUid(cu.getUsername());            
            lf.setTime_span(this.time_span);
            lf.setReason(this.reason);
            lf.setName(personService.find(cu.getUsername()).getName());
            lf.setTeacherOpinion(this.teacher_opinion);
            lf.setStudentOfficeOpinion(this.student_office_opinion);
            lf.setEducationOfficeOpinion(this.education_office_opinion);
            lf.setSchoolOpinion(this.school_opinion);
            officeMgr.apply(cu.getUsername(), lf);
            logger.log("WAR: reason - " + this.reason);
            this.logger.log("WAR: leave form"+this.name+this.id);
            this.logger.log("WAR: leave form"+lf.getName()+lf.getUid());
        } else if (myParam.equals("examine")) {
            logger.log("WAR: taskid - " + mytaskid);
            TaskPack taskpack = officeMgr.getTask(mytaskid, cu.getUsername());
            LeaveForm lf = (LeaveForm) taskpack.getForm();
            //lf.setName(this.name);
            lf.setTime_span(this.time_span);
            lf.setTeacherOpinion(this.teacher_opinion);
            lf.setStudentOfficeOpinion(this.student_office_opinion);
            lf.setEducationOfficeOpinion(this.education_office_opinion);
            lf.setSchoolOpinion(this.school_opinion);
            lf.setReason(this.reason);
            logger.log("before submit----"+this.time_span+this.teacher_opinion+this.student_office_opinion+this.education_office_opinion+this.school_opinion);
            boolean approv = this.decision.equals("agree");
            officeMgr.submitTask(mytaskid, approv, lf);
            logger.log("after submit----"+lf.getName()+lf.getUid()+lf.getTime_span()+lf.getTeacherOpinion()+lf.getStudentOfficeOpinion()+lf.getEducationOfficeOpinion()+lf.getSchoolOpinion());
        } else {
        }
        return "home";
    }

    public String commitForm(String s, String id) {
        CurrentUser cu = (CurrentUser) getSessionMap().get("currentUser");
        String outcome = null;
        LeaveForm lf = new LeaveForm();
        lf.setTime_span(this.time_span);
        lf.setReason(this.reason);
        lf.setName(this.name);


        if (s.equals("apply")) {
            officeMgr.apply(cu.getUsername(), lf);
            logger.log(this.reason);

        } else if (s.equals("examine")) {
            officeMgr.submitTask(id, true, lf);
        } else {
        }

        return "home";
    }

    public String setPermission() {
        String[] s = {"false", "true", "true", "true", "true"};
        this.setDisabled(s);

        return "leave_form";
    }

    public String fillForm(String s, String taskid) {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest myRequest = (HttpServletRequest) context.getExternalContext().getRequest();
        //      HttpSession mySession = myRequest.getSession(); 
        String mytaskid = myRequest.getParameter("mytaskid");
        CurrentUser cu = (CurrentUser) getSessionMap().get("currentUser");
        logger.log(mytaskid);
        logger.log(taskid);
        if (s.equals("apply")) {

            this.setPermission();
            this.setId(cu.getUsername());
            this.setName(personService.find(cu.getUsername()).getName());
        }else if (s.equals("examine")) {
            TaskPack taskpack = officeMgr.getTask(taskid, cu.getUsername());
            LeaveForm leaveForm = (LeaveForm) taskpack.getForm();
            this.setName(leaveForm.getName());
            this.setId(leaveForm.getUid());
            this.setTime_span(leaveForm.getTime_span());
            this.setReason(leaveForm.getReason());
            //           this.setId(leaveForm.getId());

            this.setTeacher_opinion(leaveForm.getTeacherOpinion());
            this.setStudent_office_opinion(leaveForm.getStudentOfficeOpinion());
            this.setEducation_office_opinion(leaveForm.getEducationOfficeOpinion());
            this.setSchool_opinion(leaveForm.getSchoolOpinion());
            logger.log("WAR teacher opinion" + leaveForm.getTeacherOpinion());
            logger.log("*** "+leaveForm.getUid()+leaveForm.getName()+leaveForm.getTeacherOpinion()+leaveForm.getStudentOfficeOpinion()+leaveForm.getEducationOfficeOpinion()+leaveForm.getSchoolOpinion());
        }else{
            
            ApplyPack ap=officeMgr.getApply("LeaveForm", taskid);
            LeaveForm leaveForm=(LeaveForm)ap.getForm(); 
            this.setName(leaveForm.getName());
            this.setId(leaveForm.getUid());
            this.setTime_span(leaveForm.getTime_span());
            this.setReason(leaveForm.getReason());
            //           this.setId(leaveForm.getId());

            this.setTeacher_opinion(leaveForm.getTeacherOpinion());
            this.setStudent_office_opinion(leaveForm.getStudentOfficeOpinion());
            this.setEducation_office_opinion(leaveForm.getEducationOfficeOpinion());
            this.setSchool_opinion(leaveForm.getSchoolOpinion());
        }
        return "leave_form";
    }

    public LeaveBacking() {
    }
}
